﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;

namespace GeoPlugin
{
    /// <summary>
    /// Interaction logic for NorthAmericaViewBox.xaml
    /// </summary>
    public partial class NorthAmericaViewBox : UserControl
    {
        string lastSelected = string.Empty;
        public USAInfo Info;
        public NorthAmericaViewBox()
        {
            InitializeComponent();
            Info = new USAInfo();
            Info.Opacity = 0;
            Info.Margin = new Thickness(20, 0, 20, 0);
            this.MapsHotSpot.Children.Add(Info);
            setTransparency();
            AddMapsEvent();
            LayoutRoot.MouseLeftButtonUp += new MouseButtonEventHandler(LayoutRoot_MouseLeftButtonUp);
        }

        private void setTransparency()
        {
            System.IO.Stream s = this.GetType().Assembly.GetManifestResourceStream("GeoPlugin.north_america.png");
            System.Drawing.Bitmap image = new System.Drawing.Bitmap(s);
            s.Close();
            image.MakeTransparent(System.Drawing.Color.White);
            MyImageConverter converter = new MyImageConverter();
            Image.Source = (BitmapSource)converter.Convert(image, null, null, null);
            // Image.MouseLeftButtonUp += new MouseButtonEventHandler(mouseHandler);
            Image.Opacity = 0.7;
        }

        void AddMapsEvent()
        {
            UIElementCollection canvases = (this.FindName("MapsHotSpot") as Canvas).Children;
            foreach (UIElement canv in canvases)
            {
                if (canv is Canvas)
                {
                    Canvas c = (Canvas)canv;
                    if (!string.IsNullOrEmpty(c.Name))
                    {
                        c.Cursor = Cursors.Hand;
                        ToolTip toolTip = new ToolTip { Content = c.Name };
                        c.SetValue(ToolTipService.ToolTipProperty, toolTip);
                        c.MouseLeftButtonUp += new MouseButtonEventHandler(c_MouseLeftButtonUp);
                        c.MouseMove += new MouseEventHandler(c_MouseMove);
                    }
                }
            }
        }

        void c_MouseMove(object sender, MouseEventArgs e)
        {
            Canvas c = sender as Canvas;
            ResetLastSelected();

            if (!string.IsNullOrEmpty(c.Name))
            {
                if (c.Name != lastSelected)
                {
                    HideMenu();
                }

                lastSelected = c.Name;
                SetCanvasColor(c, Color.FromArgb(255, 127, 255, 0), 2, Colors.Green);
            }
        }

        void c_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Canvas c = sender as Canvas;
            if (!string.IsNullOrEmpty(c.Name))
            {
                PopulateContextMenu(c.Name);
                PositionContextMenu(e.GetPosition(contextMenu.Parent as UIElement), true);
                e.Handled = true;
            }
        }

        void LayoutRoot_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            ResetLastSelected();
            HideMenu();
            if (Info.Opacity != 0)
            {
                Info.Opacity = 0;
            }
        }

        void HideMenu()
        {
            ((Storyboard)this.LayoutRoot.TryFindResource("HidePopup2")).Begin();
            
        }


        private void PositionContextMenu(Point p, bool useTransition)
        {
            if (useTransition)
                contextMenu.IsOpen = false;
            contextMenu.HorizontalOffset = p.X;
            contextMenu.VerticalOffset = p.Y;
            contextMenu.IsOpen = true;
        }

        void ResetLastSelected()
        {
            if (!string.IsNullOrEmpty(lastSelected))
            {
                Canvas selectedCanvas = this.FindName(lastSelected) as Canvas;
                SetCanvasColor(selectedCanvas, Color.FromArgb(255, 250, 254, 255), 1, Colors.Transparent);
            }
        }

        void SetCanvasColor(Canvas country, Color setColor, double strokeThickness, Color stroke)
        {
            Path mPath = new Path();
            mPath = country.Children[0] as Path;

            mPath.Opacity = .4;

            mPath.Stroke = new SolidColorBrush(stroke);
            mPath.Fill = new SolidColorBrush(setColor);

            mPath.StrokeThickness = strokeThickness;
        }

        private void PopulateContextMenu(string country)
        {
            contextListBox.Items.Clear();
            Button showInfoButton = new Button();
            showInfoButton.Content = "Show Info";
            showInfoButton.Name = country + "Info";
            showInfoButton.Width = 100;
            showInfoButton.Height = 65;
            contextListBox.Items.Add(showInfoButton);
            Button separatorButton = new Button();
            separatorButton.Opacity = 0;
            separatorButton.Width = 50;
            separatorButton.Height = 45;
            contextListBox.Items.Add(separatorButton);
            Button loadMapButton = new Button();
            loadMapButton.Content = "Load Map";
            loadMapButton.Name = country;
            loadMapButton.Width = 100;
            loadMapButton.Height = 65;
            loadMapButton.Click += new RoutedEventHandler(loadMapButton_Click);
            showInfoButton.Click += new RoutedEventHandler(showInfoButton_Click);
            contextListBox.Items.Add(loadMapButton);

        }

        void showInfoButton_Click(object sender, RoutedEventArgs e)
        {
            contextListBox.Items.Clear();
            this.HideMenu();
            contextMenu.HorizontalOffset = -5000.0;
            switch (((Button)sender).Name)
            {
                case "USAInfo":
                    {
                        if (Info != null)
                        {
                            Info.Opacity = 0.8;
                        }
                        break;
                    }
                default:
                    {
                        break;
                    }
            }
        }

        void loadMapButton_Click(object sender, RoutedEventArgs e)
        {

            contextListBox.Items.Clear();
            this.HideMenu();
            contextMenu.HorizontalOffset = -5000.0;
            switch (((Button)sender).Name)
            {
                case "USA":
                    {
                        MapControl mapControl = new MapControl(new UnitedStatesViewBox(), "USA");
                        ((MapControl)((Grid)this.Parent).Parent).GoToInnerMap(mapControl);
                        break;
                    }
                default:
                    {
                        MapControl mapControl = new MapControl(new UnitedStatesViewBox(), "USA");
                        ((MapControl)((Grid)this.Parent).Parent).GoToInnerMap(mapControl);
                        break;
                    }
            }
        }

        private void contextMenu_Opened(object sender, EventArgs e)
        {
            ((Storyboard)this.LayoutRoot.TryFindResource("ShowPopup2")).Begin();

        }

        private void contextMenu_Closed(object sender, EventArgs e)
        {
            ((Storyboard)this.LayoutRoot.TryFindResource("HidePopup2")).Begin();
        }
    }
}
